using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;


namespace ArchSoft
{
    public partial class ScheduleFrm : Form
    {
        DataSet1 ds = new DataSet1();
        DataSet1.DrawingScheduleRow row;

        DataSet1.ElementScheduleRow row1;

        DataSet1.DevRow row2;

        private string _folderPath;
        private string _folderPath1;
        CrystalReport2 rpt = new CrystalReport2();

        public ScheduleFrm()
        {
            InitializeComponent();
        }

        public string FolderPath
        {
            set { _folderPath = value; }
        }

        public string FolderPath1
        {
            set { _folderPath1 = value; }
        }

        public void LoadDev()
        {
            string Tbx_Dev_Appr = "";
            string Tbx_Dev_chck = "";
            string Tbx_Dev_Des = "";
            string Tbx_Dev_Drw = "";


            string Pat = this._folderPath1 + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
            if (File.Exists(Pat))
            {
                System.Data.DataTable tbConsl = null;
                DataSet ds1 = new DataSet();
                ds1.ReadXml(Pat);
                tbConsl = ds1.Tables[0];
                if (tbConsl != null)
                {
                    Tbx_Dev_Appr = EncodeDecode.GetDecoded(tbConsl.Rows[15][1].ToString());
                    Tbx_Dev_chck = EncodeDecode.GetDecoded(tbConsl.Rows[16][1].ToString());
                    Tbx_Dev_Des = EncodeDecode.GetDecoded(tbConsl.Rows[17][1].ToString());
                    Tbx_Dev_Drw = EncodeDecode.GetDecoded(tbConsl.Rows[18][1].ToString());

                    row2 = ds.Dev.NewDevRow();
                    row2[0] = Tbx_Dev_Appr;
                    row2[1] = Tbx_Dev_chck;
                    row2[2] = Tbx_Dev_Des;
                    row2[3] = Tbx_Dev_Drw;

                    ds.Dev.AddDevRow(row2);
                }
            }
            else
                return;
        }

        public void LoadScheduleInf()
        {
            //string Pat = Directory.GetParent(this._folderPath).ToString() + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
            //if (File.Exists(Pat))
            //{
            //    System.Data.DataTable tbConsl = null;
            //    DataSet ds = new DataSet();
            //    ds.ReadXml(Pat);
            //    tbConsl = ds.Tables[0];
            //    if (tbConsl != null)
            //    {
            //        Tbx_Dev_Appr = EncodeDecode.GetDecoded(tbConsl.Rows[15][1].ToString());
            //        Tbx_Dev_chck = EncodeDecode.GetDecoded(tbConsl.Rows[16][1].ToString());
            //        Tbx_Dev_Des = EncodeDecode.GetDecoded(tbConsl.Rows[17][1].ToString());
            //        Tbx_Dev_Drw = EncodeDecode.GetDecoded(tbConsl.Rows[18][1].ToString());

            //        //row[0] = Tbx_Dev_Appr;
            //        //row[1] = Tbx_Dev_chck;
            //        //row[2] = Tbx_Dev_Des;
            //        //row[3] = Tbx_Dev_Drw;
            //    }
            //}

            string Pat = _folderPath;

            string FilePath = ""; 
            string FileName = "";
            string Scale = "";
            string Revision = "";
            string Status = "";
            string Start_Date = "";
            string End_Date = "";

            if (File.Exists(Pat))
            {
                DataTable tbConsl = null;
                DataSet ds1 = new DataSet();
                ds1.ReadXml(Pat);
                tbConsl = ds1.Tables[0];


                
                
               // tbConsl.DefaultView.Sort ="[" + tbConsl.Columns[1].ColumnName + "] asc";
                for (int i = 0; i < tbConsl.Rows.Count; i++)
                {
                    FilePath = tbConsl.Rows[i][0].ToString();
                    FileName = tbConsl.Rows[i][1].ToString();
                    Scale = tbConsl.Rows[i][2].ToString();
                    Revision = tbConsl.Rows[i][3].ToString();
                    Status = tbConsl.Rows[i][4].ToString();
                    Start_Date = tbConsl.Rows[i][6].ToString();
                    End_Date = tbConsl.Rows[i][7].ToString();

                    row = ds.DrawingSchedule.NewDrawingScheduleRow();
                    row[0] = Process(FilePath);
                    row[1] = FileName;
                    row[2] = Convert.ToDateTime(Start_Date).ToShortDateString();
                    row[3] = Convert.ToDateTime(End_Date).ToShortDateString();
                   // row[4] = Status;
                    TimeSpan duration = Convert.ToDateTime(End_Date) - Convert.ToDateTime(Start_Date);
                    string days = duration.TotalDays.ToString();
                    if (duration.TotalDays > 0)
                    {
                        row[5] = days + " days";
                    }
                    else
                        row[5] = 0 + " days";
                    TimeSpan dayleft= Convert.ToDateTime(End_Date) - DateTime.Now;
                    double days1 = dayleft.TotalDays;
                    int days2 = 0;
                    if (days1 > 0)
                    {
                        if ((dayleft.TotalDays - dayleft.Days) > 0)
                        {
                            days2 = dayleft.Days + 1;
                        }
                        else
                            days2 = dayleft.Days;
                        row[6] = days2.ToString() + " days";
                    }
                    else
                        row[6] = "0 days";


                    if (days1 > 0)
                    {
                        if ((Convert.ToDouble(days) - Convert.ToDouble(days1)) > 0)
                        {
                            double per = (Convert.ToDouble(days) - Convert.ToDouble(days1)) / Convert.ToDouble(days) * 100;
                            row[7] = Math.Round(per) + " %";
                        }
                        else
                            row[7] = "0 %";
                    }
                    else
                        row[7] = "100 %";

                    if (row[7] == "100 %")
                    {
                        row[4] = "Finish";
                    }
                    else if (row[7] == "0 %")
                    {
                        row[4] = "No Started";
                    }
                    else row[4] = "In Processing";

                    string element = Process(FilePath);
                    string sort = element.Substring(1, 1);
                    int sort_number = Convert.ToInt32(sort) + 1;
                    row[8] = sort_number;
                    ds.DrawingSchedule.AddDrawingScheduleRow(row);

                    //row1 = ds.ElementSchedule.NewElementScheduleRow();
                    //row1[0] = Process(FilePath);
                    //ds.ElementSchedule.AddElementScheduleRow(row1);
                }

                
                //int sumElement = 0;
                //for (int i = 0; i < ds.DrawingSchedule.Rows.Count; i++)
                //{
                    
                //   string start =   ds.DrawingSchedule.Rows[i][6].ToString();
                //   string end = ds.DrawingSchedule.Rows[i][7].ToString();
                //}
            }
            else
            {
                return;
            }
        }

        private static void SortDataTable(DataTable dt, string sort)
        {
            DataTable newDT = dt.Clone();
            int rowCount = dt.Rows.Count;

            DataRow[] foundRows = dt.Select(null, sort); // Sort with Column name
            for (int i = 0; i < rowCount; i++)
            {
                object[] arr = new object[dt.Columns.Count];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    arr[j] = foundRows[i][j];
                }
                DataRow data_row = newDT.NewRow();
                data_row.ItemArray = arr;
                newDT.Rows.Add(data_row);
            }

            //clear the incoming dt
            dt.Rows.Clear();

            for (int i = 0; i < newDT.Rows.Count; i++)
            {
                object[] arr = new object[dt.Columns.Count];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    arr[j] = newDT.Rows[i][j];
                }

                DataRow data_row = dt.NewRow();
                data_row.ItemArray = arr;
                dt.Rows.Add(data_row);
            }

        }

        public string Process(string Element)
        {
                    string[] a = Element.Split(Path.DirectorySeparatorChar);
                    string kq = a[a.Length - 2];
                    return kq;
        }

        public void addBlankRow(int i)
        {
            DataSet1.DrawingScheduleRow row = ds.DrawingSchedule.NewDrawingScheduleRow();
            row[0] = string.Empty;
            row[1] = string.Empty;
            row[2] = string.Empty;
            row[3] = string.Empty;
            row[4] = string.Empty;
            row[5] = string.Empty;
            row[6] = string.Empty;
            row[7] = string.Empty;
            //row[8] = string.Empty;
            ds.DrawingSchedule.Rows.InsertAt(row, i);
        }

        public void SaveToXML()
        {
            string Pat = _folderPath;
            if (File.Exists(Pat))
            {
                DataTable tbConsl = null;
                DataSet ds1 = new DataSet();
                ds1.ReadXml(Pat);
                tbConsl = ds1.Tables[0];

                for (int i = 0; i < tbConsl.Rows.Count; i++)
                {
                    string filename = tbConsl.Rows[i][1].ToString();
                    string filename1 = ds.DrawingSchedule.Rows[i][1].ToString();

                    
                    if(filename == filename1)
                    {
                        string status = tbConsl.Rows[i][4].ToString();
                        string status1 = ds.DrawingSchedule.Rows[i][4].ToString();

                        tbConsl.Rows[i][4] = status1;
                    }
                }

                DataSet dsxml = new DataSet();
                dsxml.Tables.Clear();
                dsxml.Tables.Add(tbConsl.Copy());
                dsxml.WriteXml(Pat);
                dsxml.Dispose();
            }
        }

        private void ScheduleFrm_Load(object sender, EventArgs e)
        {
            //rpt.SetDataSource(ds);
            SaveToXML();
            //ds.Tables[2].DefaultView.Sort = ds.Tables[2].Columns[8].ColumnName ;
            SortDataTable(ds.DrawingSchedule, "D_Sort");

            for (int i = 0; i < ds.DrawingSchedule.Rows.Count; i++)
            {
                if ((i + 1) < ds.DrawingSchedule.Rows.Count)
                {
                    if (ds.DrawingSchedule.Rows[i][8].ToString() != ds.DrawingSchedule.Rows[i + 1][8].ToString())
                    {
                        addBlankRow(i + 1);
                        i = i + 1;
                    }
                }
            }

            //DataSet1.DrawingRow row;


            
            rpt.SetDataSource(ds);
            crystalReportViewer1.ReportSource = rpt;
        }
    }
}